package com.imchen.cms.aop;

import com.imchen.cms.annotation.LogTrack;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author imchen
 * @create: 2019-01-12 18:33
 **/
@Aspect
@Component
public class LogTrackAspect {

    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Pointcut("@annotation( com.imchen.cms.annotation.LogTrack)")
    public void logTracking(){}

    @Before("logTracking()")
    public void doBefore(JoinPoint joinPoint){
        logger.debug("Aspect before:{}",joinPoint.getArgs());
        MethodSignature signature= (MethodSignature) joinPoint.getSignature();
        Method method =signature.getMethod();
        LogTrack logTrack=method.getAnnotation(LogTrack.class);
        if (logTrack!=null){
            logger.debug("Aspect value:{}",logTrack.value());
        }
    }

    @After("logTracking()")
    public void doAfter(){
//        logger.debug("Aspect after: {}");
    }

    @AfterReturning(returning = "object",pointcut = "logTracking()")
    public void doAfterReturn(Object object){
//        logger.debug("Aspect after return object:{}",object.getClass().getName());
    }
}